<?php

/**
 * This is the model class for table "tbl_film".
 *
 * The followings are the available columns in table 'tbl_film':
 * @property string $id
 * @property string $name_vn
 * @property string $name_en
 * @property string $link
 * @property string $poster
 * @property string $description
 * @property string $ibm_rating
 * @property string $year
 * @property string $runtime
 * @property string $quality
 * @property integer $view_cnt
 * @property string $trailer_url
 * @property integer $type
 * @property string $user_create
 * @property string $create_at
 * @property string $keyword
 * @property string $note
 * @property string $director_id
 * @property integer $no_episode
 * @property integer $status
 *
 * The followings are the available model relations:
 * @property Episode[] $episodes
 * @property User[] $users_favorite
 * @property User $user_create
 * @property Actor[] $actors
 * @property Director[] $directors
 * @property Country[] $countries
 * @property Menu[] $menus
 * @property User[] $users_rating
 * @property Tag[] $tags
 * @property User[] $users_view
 * @property User[] $users_watched
 */
class Film extends CActiveRecord {

    const PHIM_LE = 0;
    const PHIM_BO = 1;

    public $actor;
    public $director;
    public $country;
    public $tag;
    public $genre;

//    public $isCompleted;

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'tbl_film';
    }

    /**
     * @return string the associated database table name
     */
    public function getFilmStatus() {
        if ($this->status==0) {
            return 0;
        }
        $episode = Episode::model()->findAll("film_id=:id and status=1",array(':id'=>  $this->id));
        if ($this->no_episode==  sizeof($episode)) {
            return 1;
        }else{         
            return 2;
        }
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('name_vn, genre, actor, director, country, tag, poster, description, ibm_rating, year, runtime, quality, user_create, create_at', 'required'),
            array('view_cnt, type, no_episode, status', 'numerical', 'integerOnly' => true),
            array('name_vn, name_en, poster', 'length', 'max' => 100),
            array('link, trailer_url, keyword', 'length', 'max' => 200),
            array('description', 'length', 'max' => 400),
            array('ibm_rating', 'length', 'max' => 50),
            array('year', 'length', 'max' => 10),
            array('runtime, quality, user_create', 'length', 'max' => 20),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('id, name_vn, name_en, link, poster, description, ibm_rating, year, runtime, quality, view_cnt, trailer_url, type, user_create, create_at, keyword, no_episode, status', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'episodes' => array(self::HAS_MANY, 'Episode', 'film_id'),
            'users_favorite' => array(self::MANY_MANY, 'User', 'tbl_favorite_film(film_id, user_id)'),
            'user_created' => array(self::BELONGS_TO, 'User', 'user_create'),
            'actors' => array(self::MANY_MANY, 'Actor', 'tbl_film_actor(film_id,actor_id)'),
            'directors' => array(self::MANY_MANY, 'Director', 'tbl_film_director(film_id, director_id)'),
            'countries' => array(self::MANY_MANY, 'Country', 'tbl_film_country(film_id, country_id)'),
            'menus' => array(self::MANY_MANY, 'Menu', 'tbl_film_genre(film_id, menu_id)'),
            'users_rating' => array(self::MANY_MANY, 'User', 'tbl_film_rating(film_id, user_id)'),
            'tags' => array(self::MANY_MANY, 'Tag', 'tbl_film_tag(film_id, tag_id)'),
            'users_view' => array(self::MANY_MANY, 'User', 'tbl_user_view(film_id, user_id)'),
            'users_watched' => array(self::MANY_MANY, 'User', 'tbl_user_watched(film_id, user_id)'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'name_vn' => 'Tên tiếng việt',
            'name_en' => 'Tên tiếng anh',
            'link' => 'Link',
            'poster' => 'Poster',
            'description' => 'Description',
            'ibm_rating' => 'Ibm Rating',
            'year' => 'Year',
            'runtime' => 'Thời gian',
            'quality' => 'Chất lượng',
            'view_cnt' => 'Lượt xem',
            'trailer_url' => 'Trailer',
            'type' => 'Kiểu phim',
            'user_create' => 'User Create',
            'create_at' => 'Tạo lúc',
            'keyword' => 'Keyword',
            'note' => 'Note',
            'no_episode' => 'Số tập',
            'status' => 'Status',
            'actor' => 'Diễn viên',
            'director' => 'Đạo diễn',
            'country' => 'Quốc gia',
            'tag' => 'Tag',
            'genre' => 'Thể loại'
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     *
     * Typical usecase:
     * - Initialize the model fields with values from filter form.
     * - Execute this method to get CActiveDataProvider instance which will filter
     * models according to data in model fields.
     * - Pass data provider to CGridView, CListView or any similar widget.
     *
     * @return CActiveDataProvider the data provider that can return the models
     * based on the search/filter conditions.
     */
    public function search() {
        // @todo Please modify the following code to remove attributes that should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id, true);
        $criteria->compare('name_vn', $this->name_vn, true);
        $criteria->compare('name_en', $this->name_en, true);
        $criteria->compare('link', $this->link, true);
        $criteria->compare('poster', $this->poster, true);
        $criteria->compare('description', $this->description, true);
        $criteria->compare('ibm_rating', $this->ibm_rating, true);
        $criteria->compare('year', $this->year, true);
        $criteria->compare('runtime', $this->runtime, true);
        $criteria->compare('quality', $this->quality, true);
        $criteria->compare('view_cnt', $this->view_cnt);
        $criteria->compare('trailer_url', $this->trailer_url, true);
        $criteria->compare('type', $this->type);
        $criteria->compare('user_create', $this->user_create, true);
        $criteria->compare('create_at', $this->create_at, true);
        $criteria->compare('keyword', $this->keyword, true);
        $criteria->compare('note', $this->note, true);
        $criteria->compare('no_episode', $this->no_episode);
        $criteria->compare('status', $this->status);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return Film the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

}
